package com.puman.beautyfarmcounselor.ui.customer;

import android.os.PersistableBundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;

import com.puman.beautyfarmcounselor.MainActivity;
import com.puman.beautyfarmcounselor.R;
import com.puman.beautyfarmcounselor.ui.home.MemberVipFragment_;
import com.puman.beautyfarmcounselor.util.MethedUtil;

import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;

@EActivity(R.layout.activity_customer)
public class CustomerActivity extends FragmentActivity {

    public static CustomerActivity customerActivity;
    private static FragmentManager fragmentManager;
    private Fragment mContent;



    public static CustomerActivity getInstace() {
        return customerActivity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        customerActivity = this;
        fragmentManager = getSupportFragmentManager();
//        if (savedInstanceState != null) {
//            mContent = getSupportFragmentManager().getFragment(
//                    savedInstanceState, "mContent");
//        }
        if (mContent == null) {
            mContent = new CustomerDetailFragment_();

        }
// else{
//            fragmentManager.beginTransaction()
//                    .replace(R.id.container, mContent)
//                    .commit();
//        }

    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        getSupportFragmentManager().putFragment(outState, "mContent",
                mContent);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub

        if( keyCode == KeyEvent.KEYCODE_BACK){
            MethedUtil.UtilLog("count:"+fragmentManager.getBackStackEntryCount());
            if (fragmentManager.getBackStackEntryCount() != 0){
                fragmentManager.popBackStack();
            }else{
                finish();
            }
        }
        return true;
    }

    /**
     *
     * @param from  当前fragment
     * @param toFragment 跳转到的fragment
     */
    public void switchFragment(Fragment from, Fragment toFragment){
        getSupportFragmentManager()
                .beginTransaction()
                .hide(from).replace(R.id.container, toFragment).addToBackStack(null).commit();
    }
}
